import json
import time

import MySQLdb
import pymysql

# connect = MySQLdb.connect(
#      # 主机
#      host='localhost',
#      # 端口号
#      port=3306,
#      # 用户名
#      user='root',
#      # 密码
#      passwd='123456',
#      # 数据库名称
#      db='students',
#      # 指定字符的编、解码格式
#      use_unicode=True,
#      charset='utf8')

# connect = pymysql.connect(
#     host='localhost',
#     port=3306,
#     user='root',
#     password='123456',
#     database='students',
#     charset='utf8'
# )
#
# # 获取游标，进行相应 SQL 操作
# cursor = connect .cursor()
#
# # 执行新增 SQL 语句
# sql = 'insert into student (name, age) values(%s,%s);'
# data = [('张三', '22'),('李四', '23')]
# cursor.executemany(sql, data)
#
# # 提交
# connect.commit()
#
# # 查询
# sql='SELECT * FROM student'
# cursor.execute(sql)
# print(cursor.fetchall())
#
# # 修改
# sql='UPDATE student SET name="haha" WHERE age=22'
# cursor.execute(sql)
#
# # 提交
# connect.commit()
#
# # 查询
# sql='SELECT * FROM student'
# cursor.execute(sql)
# print(cursor.fetchall())
#
# # 删除
# sql='DELETE FROM student' #全表删除
# cursor.execute(sql)
#
# # 提交
# connect.commit()
#
# # 查询
# sql='SELECT * FROM student'
# cursor.execute(sql)
# print(cursor.fetchall())
#
# # 关闭
# cursor.close()
# connect.close()


# json序列化
d = {'id':'001', 'name':'张三', 'age':'20'}
j = json.dumps(d, ensure_ascii=False)
print(j)

# time.sleep(10)

j = json.dumps(d, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ' : '))
print(j)

# # 向文件中写入数据
# with open('test.json', 'w', encoding='utf-8') as f:
#     f.write(j)

# with open('test.json', 'w', encoding='utf-8') as f:
#     json.dump(d, f, indent=4, ensure_ascii=False)

# json反序列化
k=json.loads(j)
print(k)

# 读取json文件中的数据进行反序列化
# with open('test.json', encoding='utf-8') as f:
#     data = f.read()
#     print(json.loads(data))

with open('test.json', encoding='utf-8') as f:
    print(json.load(f))